import React from 'react'
import { RouterArr } from '@/routes/index'
import {
  UserOutlined,
  KeyOutlined,
  AppstoreOutlined,
  TableOutlined,
  AreaChartOutlined,
  MailOutlined
} from '@ant-design/icons'

/**
 * @description 获取浏览器默认语言
 * @return string
 */
const IconMap: any = {
  '125': <UserOutlined />,
  '103': <KeyOutlined />,
  '101': <AppstoreOutlined />,
  '102': <TableOutlined />,
  '145': <AreaChartOutlined />
}
export const getBrowserLang = () => {
  const browserLang = navigator.language ? navigator.language : navigator.browserLanguage
  let defaultBrowserLang = ''
  if (
    browserLang.toLowerCase() === 'cn' ||
    browserLang.toLowerCase() === 'zh' ||
    browserLang.toLowerCase() === 'zh-cn'
  ) {
    defaultBrowserLang = 'zh'
  } else {
    defaultBrowserLang = 'en'
  }
  return defaultBrowserLang
}

/**
 * @description 递归查询对应的路由
 * @param {String} path 当前访问地址
 * @param {Array} routes 路由列表
 * @returns array
 */
export const searchRoute = (path: string, routes: RouterArr[] = []) => {
  let result: RouterArr = {}
  for (const item of routes) {
    if (item.path === path) return item
    if (item.children) {
      // 递归查询子路由
      const res = searchRoute(path, item.children)
      if (Object.keys(res).length) result = res
    }
  }
  return result
}

/**
 * @description 递归查询左侧导航菜单
 *  @param { Array } menus 当前获取到的左侧菜单
 *  @returns array
 */

export const searchLeftMenus = (menus: any) => {
  return menus.map((item: any, index: number) => {
    if (item.children.length) {
      const newArr = item.children.reduce((prev: any, cur: any) => {
        return prev.concat([
          {
            label: cur.authName,
            key: cur.path,
            icon: <MailOutlined />,
            children: undefined
          }
        ])
      }, [])
      return {
        label: item.authName,
        key: item.path + 'parent',
        icon: IconMap[item.id],
        children: newArr
      }
    }
  })
}
/**
 * @description 时间戳转换为日期时间格式
 *  @param { number } timestamps 传入的时间戳
 *  @returns string
 */

export const formmatDateTime = (timestamps: number) => {
  if (!timestamps) return null
  const dt = new Date(timestamps)
  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')
  const hh = (dt.getHours() + '').padStart(2, '0')
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  const ss = (dt.getSeconds() + '').padStart(2, '0')
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}
/**
 * @description 时间戳转换为日期格式
 *  @param { number } timestamps 传入的时间戳
 *  @returns string
 */

export const formmatDateNoTime = (timestamps: number) => {
  if (!timestamps) return null
  const dt = new Date(timestamps)
  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')
  return `${y}-${m}-${d}`
}
